<template>
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    v-if="!IsFollowed"
    @click="onFollowed"
    :loading="isFollowLoading"
  >关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    round
    size="small"
    @click="onFollowed"
    :loading="isFollowLoading"
  >已关注</van-button>
</template>

<script>
import { DeleteTarget, PostFollowings } from '@/api/user'
export default {
  props: {
    IsFollowed: {
      type: Boolean,
      required: true
    },
    AutId: {
      type: [Number, Object, String],
      required: true
    }
  },
  model: {
    prop: 'IsFollowed',
    event: 'updateFollowed'
  },
  data () {
    return {
      isFollowLoading: false
    }
  },

  created () {

  },

  methods: {
    async  onFollowed () {
      this.isFollowLoading = true
      try {
        if (this.IsFollowed) {
        // this.articles.is_followed===true 时说明已经关注了
          console.log('点击了取消关注')
          await DeleteTarget(this.AutId)
        } else {
        // 反之没有关注
          console.log('点击了关注')
          await PostFollowings(this.AutId)
        }
        this.$emit('updateFollowed', !this.IsFollowed)
        this.isFollowLoading = false
      } catch (err) {
        // 失败了
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
